package Dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import common.Dbutil;
import vo.Goods;
import vo.User;

public class Gooddao {
	private Goods good;
	private Dbutil db;
	public Gooddao(){
		
	}
	public Gooddao(Goods good){
		this.good = good;
	}
	public Goods getGood() {
		return good;
	}
	public void setGood(Goods good) {
		this.good = good;
	}
	public Dbutil getDb() {
		return db;
	}
	public void setDb(Dbutil db) {
		this.db = db;
	}
	public int do_register(Goods go){
		int result = -1;
		db = new Dbutil();
		String sql = "insert into goods(good_name,good_price,ispreferred,good_desc,good_pic,qty) values('"+go.getGood_name()+"',"+go.getGood_price()+","+go.getIspreferred()+",'"+go.getGood_desc()+"','"+go.getGood_pic()+"',"+go.getQty()+");";
		result = db.executeUpdate(sql);
		db.closeDB();
		return result;
	}	
	/**
	 * 查询
	 * @param uname
	 */
	public ArrayList<Goods> do_Select(){
		ArrayList<Goods> list = new ArrayList<Goods>();
		db = new Dbutil();
		String sql = "select good_name,good_price,ispreferred,good_desc,good_pic from goods;";
		ResultSet rs = db.executeQuery(sql);
		try {
			while(rs.next()){
			Goods go = new Goods();
			go.setGood_name(rs.getString("good_name"));
			go.setGood_price(rs.getString("good_price"));
			go.setIspreferred(rs.getInt("ispreferred"));
			go.setGood_desc(rs.getString("good_desc"));
			go.setGood_pic(rs.getString("good_pic"));
			list.add(go);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			rs.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		db.closeDB();
		return list;
	}
	/**
	 * 查询
	 * @param uname
	 */
	public ArrayList<Goods> do_Select_bypage(int page){
		ArrayList<Goods> list = new ArrayList<Goods>();
		db = new Dbutil();
		String sql = "select good_id,good_name,good_price,ispreferred,good_desc,good_pic,type_id,qty from goods limit "+page+",3;";
		ResultSet rs = db.executeQuery(sql);
		try {
			while(rs.next()){
			Goods go = new Goods();
			go.setGood_id(rs.getInt("good_id"));
			go.setGood_name(rs.getString("good_name"));
			go.setGood_price(rs.getString("good_price"));
			go.setIspreferred(rs.getInt("ispreferred"));
			go.setGood_desc(rs.getString("good_desc"));
			go.setGood_pic(rs.getString("good_pic"));
			go.setType_id(rs.getInt("type_id"));
			go.setQty(rs.getInt("qty"));
			list.add(go);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			rs.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		db.closeDB();
		return list;
	}
	/**
	 * 更改商品的上架type
	 * @param 
	 * @return
	 */
	public int do_change(String id,String tic){
		int result = -1;
		db = new Dbutil();
		String sql = "update goods set type_id = "+tic+" where good_id = "+id+";";
		result = db.executeUpdate(sql);
		db.closeDB();
		return result;
	}
	/**
	 * 删除商品
	 * @param 
	 * @return
	 */
	public int do_delete(String id){
		int result = -1;
		db = new Dbutil();
		String sql = "delete from goods where good_id = "+id+";";
		result = db.executeUpdate(sql);
		db.closeDB();
		return result;
	}	

}
